<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Model\Wechat;
use Illuminate\Support\Facades\Redirect;

use App\Ext\Wechat\Common;
use App\Ext\Wechat\User;

class WechatController extends Controller
{
    protected static $type = [
        1=>'订阅号',
        2=>'服务号',
        3=>'认证订阅号',
        4=>'认证服务号'
    ];

    public function index() {
        $wechat = null;
        $wechat = Wechat::where('id',1)->first();
//        dump($wechat);
        $type = self::$type;

        return view('manage.wechat.index',compact('type','wechat'));
    }

    public function store() {
        $id = Input::get('id');
        $name = Input::get('name');
        $type = Input::get('type');
        $app_id = Input::get('app_id');
        $app_secret = Input::get('app_secret');
        if(empty($name) || empty($type) || empty($app_id) || empty($app_secret)) {
            return redirect()->back()->withInput()->withErrors('缺少参数!');
        }
        if(empty($id)) {
            $map = Wechat::insert([
                'name' => $name,
                'type' => $type,
                'app_id' => $app_id,
                'app_secret' => $app_secret,
                'create_user' => session('user.id')
            ]);
        }
        else {
            $map = Wechat::where('id',$id)->update([
                'name' => $name,
                'type' => $type,
                'app_id' => $app_id,
                'app_secret' => $app_secret,
                'create_user' => session('user.id')
            ]);
        }
        if($map) {
            return Redirect::to('manage/wechat');
        }
        else {
            return redircet()->back()->withInput()->withErrors('添加失败!');
        }
    }
}
